<?php
	include_once dirname(dirname(dirname(__FILE__))).'/include/db_connect.php';


	function getGoodsAllInfo(){

	$conn=get_db_conn();
	$sql='select g.*,gc.id as gc_id,gc.name gc_name from goods g left join goods_class gc on g.goods_class_id=gc.id  order by gc.name';
	$result=mysql_query($sql,$conn) or die (mysql_error());
	mysql_close($conn);
	return $result;	
	}


	function InitArray(){

		$ret=getGoodsAllInfo();
		$arry= array();
		$i=0;
		while ($row=mysql_fetch_array($ret)) {
			$arry[$i]["name"]=$row["name"];
			$arry[$i]["price"]=$row["price"];
			$arry[$i]["number"]=$row["number"];//每箱数目
			$arry[$i]["goods_class"]=$row["gc_name"];
			$arry[$i]["goods_class_id"]=$row["gc_id"];
			$arry[$i]["count"]="";//入货数
			$i++;
		}
		return $arry;

	}


	function InitClassArray(){
		$ret=getGoodsClass();
		$arry = array();
		$i=0;
		while ($row=mysql_fetch_array($ret)) {
			$arry[$i]["class"]=$row["name"];
			$arry[$i]["goods_num"]=getGoodsCountByClassId($row["id"]);
			$i++;
		}
		return $arry;

	}

	function getGoodsFormContent($date){

		$conn=get_db_conn();
		$sql='select content,class_content from goods_form where form_date='.'"'.$date.'"';
		$result=mysql_query($sql,$conn) or die (mysql_error());
		$ret=mysql_fetch_array($result);
		mysql_close($conn);
		return $ret;

	}

	function getGoodsClass(){
	$conn=get_db_conn();
	$sql='select * from goods_class order by name';
	$result=mysql_query($sql,$conn) or die (mysql_error());
	mysql_close($conn);
	return $result;	

	}
	function getGoodsByClassId($class_id){

	$conn=get_db_conn();
	$sql='select * from goods where goods_class_id='.$class_id;
	$result=mysql_query($sql,$conn) or die (mysql_error());
	mysql_close($conn);
	return $result;	



	}

	function getGoodsCount(){

	$conn=get_db_conn();
	$sql='select count(*) as cnt from goods';
	$result=mysql_query($sql,$conn) or die (mysql_error());
	$ret = mysql_fetch_array($result);
	mysql_close($conn);
	return $ret["cnt"];	



	}
	function getGoodsCountByClassId($class_id){
	$conn=get_db_conn();
	$sql='select count(*) as cnt from goods where goods_class_id='.$class_id;
	$result=mysql_query($sql,$conn) or die (mysql_error());
	$ret = mysql_fetch_array($result);
	mysql_close($conn);
	return $ret["cnt"];	


	}
	function getFormDate(){
		$conn=get_db_conn();
		$sql='select form_date from goods_form order by form_date desc';
		$result=mysql_query($sql,$conn) or die (mysql_error());
		mysql_close($conn);
		return $result;
	}

	function addGoodsForm($params){
	$col="";
	$val="";
	foreach ($params as $key => $item) {
		$col.=",".$key;
		$val.=",".$item;
	}
	$sql = "insert into goods_form(".substr($col, 1).") values (".substr($val, 1).");";
	$conn = get_db_conn();
	if(mysql_query($sql,$conn))return true;
	else  {
		echo $sql;
		return  mysql_error();
	}

	}
?>